/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import connectivity.DbManager;
import connectivity.QueryManager;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import main.BarApplication;
import model.Member;
import model.Staff;


/**
 *
 * /**
 *
 * @author Home
 */
public class LoginScreen extends javax.swing.JPanel {

    /**
     * Creates new form LoginScreen
     */
    private DbManager dbManager;
    private QueryManager queryManager;
    public String soortRecht;
    public int gebruikersNummer;

    public int getGebruikersNummer() {
        return gebruikersNummer;
    }

    public void setGebruikersNummer(int gebruikersNummer) {
        this.gebruikersNummer = gebruikersNummer;
        System.out.println("id =" + gebruikersNummer);
    }

    public String getSoortRecht() {
        return soortRecht;
    }

    public void setSoortRecht(String soortRecht) {
        this.soortRecht = soortRecht;
        System.out.println("functie =" + soortRecht);
    }

    public LoginScreen() {
        initComponents();
        // Aanmaken connectie
        dbManager = new DbManager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);
    }

    private void handelsregister() {
        //Definieren variabelen  
        Staff medewerker;
        Member lid;
        soortRecht = "";


        // Zoeken van medewerker aan de hand van medewerkernummer
        medewerker = queryManager.getMedewerker(Integer.parseInt(jTextFieldGebruikersNummer.getText()));
        //Controleren of medewerker gevonden is.
        if (medewerker != null) {
            if (medewerker.getStaffId() >= 1) {
                //Controleren of medewerkernummer en wachtwoord overeenkomen
                medewerker = queryManager.getMedewerkerWachtWoord(Integer.parseInt(jTextFieldGebruikersNummer.getText()), String.valueOf(jPasswordFieldWachtWoord.getPassword()));
                if (medewerker == null) {

                    // Zoeken van lid aan de hand van lidnummer
                    lid = queryManager.getMember(Integer.parseInt(jTextFieldGebruikersNummer.getText()));
                    //Controleren of medewerker gevonden is.
                    if (lid.getMemberId() >= 1) {
                        //Controleren of medewerkernummer en wachtwoord overeenkomen
                        lid = queryManager.getLidWachtWoord(Integer.parseInt(jTextFieldGebruikersNummer.getText()), String.valueOf(jPasswordFieldWachtWoord.getPassword()));
                        if (lid == null) {
                            JOptionPane.showMessageDialog(this, "Dit wachtwoord is niet juist. Probeer het opnieuw?");
                        } else {
                            soortRecht = "lid";
                            Member member = BarApplication.getQueryManager().getMember(Integer.parseInt(jTextFieldGebruikersNummer.getText()));

                            BarApplication.getInstance().showPanel(new MemberDetail(member));

                        }

                    } else {
                        JOptionPane.showMessageDialog(this, "Het gebruikersnummer is niet juist. Probeer het opnieuw?");
                    }

                    //JOptionPane.showMessageDialog(this, "Dit wachtwoord is niet juist. Probeer het opnieuw?");
                } else {
                    // Op dit moment 2 rechten. Het gaat om administratief medewerker = 1 en baliemedewerker = 2
                    if (medewerker.getFunctionId() == 1) {
                        soortRecht = "Manager";
                       // Staff staff = BarApplication.getQueryManager().getMedewerker(Integer.parseInt(jTextFieldGebruikersNummer.getText()));
                        BarApplication.getInstance().showPanel(new Home(this));
                    } else {
                        soortRecht = "Medewerker";
                        BarApplication.getInstance().showPanel(new Home(this));

                    }
                }
            }
        } else {
            // Zoeken van lid aan de hand van lidnummer
            lid = queryManager.getMember(Integer.parseInt(jTextFieldGebruikersNummer.getText()));
            //Controleren of medewerker gevonden is.
            if (lid != null) {
                if (lid.getMemberId() >= 1) {
                    //Controleren of medewerkernummer en wachtwoord overeenkomen
                    lid = queryManager.getLidWachtWoord(Integer.parseInt(jTextFieldGebruikersNummer.getText()), String.valueOf(jPasswordFieldWachtWoord.getPassword()));
                    if (lid == null) {
                        JOptionPane.showMessageDialog(this, "Dit wachtwoord is niet juist. Probeer het opnieuw?");
                    } else {
                        soortRecht = "lid";
                            Member member = BarApplication.getQueryManager().getMember(Integer.parseInt(jTextFieldGebruikersNummer.getText()));

                            BarApplication.getInstance().showPanel(new MemberDetail(member));

                    }

                } else {
                    JOptionPane.showMessageDialog(this, "Het gebruikersnummer is niet juist. Probeer het opnieuw?");
                }
            } else {

                JOptionPane.showMessageDialog(this, "Het gebruikersnummer is niet juist. Probeer het opnieuw?");
            }
        }
        if (!"".equals(soortRecht)) {

            setGebruikersNummer(Integer.parseInt(jTextFieldGebruikersNummer.getText()));
            setSoortRecht(soortRecht);
            dbManager.closeConnection();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPasswordFieldWachtWoord = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldGebruikersNummer = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        Annulren_btn = new javax.swing.JButton();

        jPanel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(102, 102, 102));
        jLabel2.setText("Password");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(102, 102, 102));
        jLabel1.setText("ID");

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/user-password-icon.png"))); // NOI18N

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon-16-protected.png"))); // NOI18N
        jButton1.setText("Log in");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButton1KeyPressed(evt);
            }
        });

        Annulren_btn.setText("Annuleren");
        Annulren_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Annulren_btnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jPasswordFieldWachtWoord)
                            .addComponent(jLabel1)
                            .addComponent(jTextFieldGebruikersNummer, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Annulren_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldGebruikersNummer)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPasswordFieldWachtWoord, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                        .addGap(37, 37, 37))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Annulren_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(103, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        handelsregister();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void Annulren_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Annulren_btnActionPerformed

        BarApplication barApplication = new BarApplication();

        barApplication.exit();

    }//GEN-LAST:event_Annulren_btnActionPerformed

    private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButton1KeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
               handelsregister();
        }

    }//GEN-LAST:event_jButton1KeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Annulren_btn;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPasswordFieldWachtWoord;
    private javax.swing.JTextField jTextFieldGebruikersNummer;
    // End of variables declaration//GEN-END:variables
}
